<?php

/*前台基础控制器
 * @Descripttion: 
 * @Author: 荷逸
 * @email: 563428234@qq.com
 * @Date: 2021-03-08 18:32:54
 */


namespace app\index\controller;

use app\common\controller\BaseController;
use app\common\model\User;
use app\index\traits\{IndexAuth, IndexTree};

class Controller extends BaseController
{
    use IndexAuth, IndexAuth;

    /**
     * 当前url
     * @var string
     */
    protected $url;

    /**
     * 当前用户ID
     * @var int
     */
    protected $uid = 0;

    /**
     * 当前用户
     * @var User
     */
    protected $user;

    /**
     * 无需验证权限的url
     * @var array
     */
    protected $authExcept = [];

    //非必要验证登录的方法，禁止在此处修改,请在具体业务Controller中修改
    protected $unnecessary = [];

    public function __call($name, $arguments)
    {
        if(request()->isAjax()){
            return error( '当前请求方法名'.$name.'不存在，请稍后再试');
        }else{
            return view('template/error.html', ['e' =>  '当前请求方法名'.$name.'不存在，请稍后再试']);

        }
    }


    protected function initialize()
    {
        $request = $this->request;
        if (!in_array($request->action(true), $this->authExcept)) {
            if (!$this->isLogin()) {
                if (in_array($this->request->action(true), $this->unnecessary)) {
                } else {
                    error('未登录', '/index/auth/login');
                }
            } else if ($this->user->id !== 1 && !$this->isLogin()) {
                error('无权限');
            }
        }

        if ((int)$request->param('check_auth') === 1) {
            success();
        }
    }

}
